package controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @date 2025/9/26
 */
@RestController
@RequestMapping("/url")
public class UrlController {

    @RequestMapping("/f1")
    public String f1(){
        System.out.println("UrLController f1");
        return "<h1>f1</h1>";
    }

    @RequestMapping(value = {"/f2","/test","/hello"})
    public String f2(){
        System.out.println("UrLController f2");
        return "<h1>f2</h1>";
    }

    @RequestMapping("/f3/*")
    public String f3(){
        System.out.println("UrLController f3");
        return "<h1>f3</h1>";
    }

    @RequestMapping("/f4/**")
    public String f4(){
        System.out.println("UrLController f4");
        return "<h1>f4</h1>";
    }

    @RequestMapping("/f5/?")
    public String f5(){
        System.out.println("UrLController f5");
        return "<h1>f5</h1>";
    }

    @RequestMapping("/f6/aa?")
    public String f6(){
        System.out.println("UrLController f6");
        return "<h1>f6</h1>";
    }

    //{xxx}表示匹配一层路径，效果相当于*
    //该方式必须与@PathVariable("key")联合使用
    //表示会将用户在{xxx}位置输入的地址值赋值给当前注解所标注的变脸
    @RequestMapping("/f7/{name}")
    public String f7(@PathVariable("name") String name){
        System.out.println("name:"+name);
        return "<h1>f7</h1>";
    }

    //@PqthVariable(key)注解中的key可以省略，默认为当前变量名
    //且该注解可以存在无数个
    @RequestMapping("/f8/{username}/{password}")
    public String f8(@PathVariable String username, @PathVariable String password){
        System.out.println("username:"+username+",password:"+password);
        return "<h1>f8</h1>";
    }

    //由于当前参数是一个Integer类型，如果用户传递的值不是一个数字，会出现400，表示无法转换
    @RequestMapping("/f9/{id}")
    public String f9(@PathVariable Integer id){
        System.out.println("id:"+id);
        return "<h1>f9</h1>";
    }

    //REST风格的请求支持正则表达式，语法:{xxx:正则表达式}
    @RequestMapping("/f10/{id:\\d+}")
    public String f10(@PathVariable Integer id){
        System.out.println("id:"+id);
        return "<h1>f10</h1>";
    }
}
